/**
 * Created by ste on 2018/1/9.
 */
/**
 * Created by Steven on 2018/1/6.
 */
import React, { Component } from 'react';
import {
    AppRegistry,
    StyleSheet,
    Text,
    View,
    TouchableOpacity,
} from 'react-native';
if (!__DEV__) {
    global.console = {
        info: () => {},
        log: () => {},
        warn: () => {},
        error: () => {},
    };
}

const styles = StyleSheet.create({
    container: {
        marginTop: 20,
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: '#F5FCFF',
    },
});

class CountDown extends Component {
    state ={
        count: 5,
        test: 9,
    };
    componentDidMount() {
        // 执行复杂操作,每秒改变定时器的值
        this.timer = setInterval(() => {
            const { count } = this.state;
            if (count === 0) {
                this.timeup();
                return clearInterval(this.timer);
            }
            this.setState({
                count: count - 1,
            });
            return 0;
        }, 1000);
    }
    timeup = () => {
        this.props.parentAdd && this.props.parentAdd('子组件传来的参数'+ this.state.test);
    }
    componentWillUnmount() {
        clearInterval(this.timer);
    }

    componentWillReceiveProps(nextProps) {
        //alert(nextProps.time)
        //this.setState({
        //  count: nextProps.time,
        //});
    }
    add = (addTime) => {
        this.setState({
            count: this.state.count + addTime,
        });
    }
    render() {
        const { count } = this.state;
        return (
            <View style={styles.container}>
                <Text>{count}</Text>
            </View>
        );
    }
}

class Shopping extends Component {
    state ={
        time: 10,
        arr: [1,2,3],
    };
    // componentWillMount() {
    //  setTimeout(() => {
    //    this.setState({
    //      time: 30,
    //    });
    //  }, 3000);
    // }
    addTime = () => {
        //const { time } = this.state;
        //this.setState({
        //  time: time + 10,
        //});
        this.CountDown.add(10);
    }
    test = () => {
        // <TouchableOpacity onPress={this.addTime} >
        //  <Text>点我添加10秒</Text>
        // </TouchableOpacity>
        // <CountDown ref={countDown => this.CountDown = countDown}  />
    }
    parentAdd = (param1,param2) => {
        alert(param1);
        alert(param2);
    }
    render() {
        return (
            <View style={styles.container}>
                {
                    this.state.arr.map(i => {
                        console.log(i);
                        return <CountDown key={i} parentAdd={(child) => this.parentAdd('父组件传来的参数:' + i,child)} />
                    })
                }
            </View>
        );
    }
}

console.ignoredYellowBox = ['Warning: Can only update'];
AppRegistry.registerComponent('Shopping', () => Shopping);